/**
 * @title: DynaSqlProvider.java
 * @package com.wehealth.weiyu.common.data.mybatis
 * @description: TODO
 * @author hushsh
 * @date 2016年12月30日下午4:59:00
 * @version V1.0
 */

package com.apiserver.data.mybatis.sqlProvider;


import com.apiserver.data.mybatis.model.EntityTable;
import com.apiserver.data.mybatis.util.EntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hs
 * @description 生成mybatis动态sql
 * @date 2016年12月30日下午4:59:00
 */
@Slf4j
public class SelProvider extends Provider {


    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 主键查询
     * @Date 下午 5:31 2019/10/8 0008
     * @Param [param]
     **/

    public <M> String selectById(Map<String, Object> param) {

        Class entityClass = (Class) param.get("entityClass");


        EntityTable entity = EntityUtil.getEntityTable(entityClass);

        SQL sql = new SQL().SELECT(entity.getSqlColumns())
                .FROM(entity.getTableName()).WHERE("1 = 1");

        String PKColumn = entity.getPKColumn();
        String PKProperty = entity.getPKProperty();

        sql = sql.WHERE(PKColumn + "=#{" + PKProperty + "}");


        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 返回查询列表, 不排序
     * @Date 下午 10:33 2019/10/10 0010
     * @Param [t]
     **/

    public <M> String selectAll(Map<String, Object> param) {
        M m = (M) param.get("m");
        EntityTable entity = EntityUtil.getEntityTable(m);

        SQL sql = new SQL()
                .SELECT(entity.getSqlColumns())
                .FROM(entity.getTableName())
                .WHERE("1 = 1");


        getWhere(m, sql, entity);

        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 返回查询列表, 排序
     * @Date 下午 10:33 2019/10/10 0010
     * @Param [t]
     **/

    public <M> String selectAllOrderBy(Map<String, Object> param) {


        M m = (M) param.get("m");

        LinkedHashMap<String, String> sortMap = (LinkedHashMap<String, String>) param.get("sortMap");


        EntityTable entity = EntityUtil.getEntityTable(m);

        SQL sql = new SQL()
                .SELECT(entity.getSqlColumns())
                .FROM(entity.getTableName())
                .WHERE("1 = 1");


        getWhere(m, sql, entity);

        getOrderSql(sortMap, sql, entity);

        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 返回排序列表，支持模糊查询，支持一属性，多个值的查询
     * @Date 下午 10:33 2019/10/10 0010
     * @Param [t]
     **/

    public <M> String selectAllByOtherKey(Map<String, Object> param) {


        M m = (M) param.get("m");
        LinkedHashMap<String, String> sortMap = (LinkedHashMap<String, String>) param.get("sortMap");
        Map<String, Object> likeQueryMap = (Map<String, Object>) param.get("likeQueryMap");
        Map<String, List<Object>> arrayQueryMap = (Map<String, List<Object>>) param.get("arrayQueryMap");


        EntityTable entity = EntityUtil.getEntityTable(m);

        SQL sql = new SQL()
                .SELECT(entity.getSqlColumns())
                .FROM(entity.getTableName())
                .WHERE("1 = 1");


        getWhere(m, sql, entity);

        getLikeWhere(likeQueryMap, sql, entity);


        getWhereForIn(arrayQueryMap, sql, entity);


        getOrderSql(sortMap, sql, entity);


        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 分组查询
     * @Date 上午 12:06 2019/10/12 0012
     * @Param [param]
     **/

    public <M> String selectAllByGroup(Map<String, Object> param) {


        M m = (M) param.get("m");
        LinkedHashMap<String, String> sortMap = (LinkedHashMap<String, String>) param.get("sortMap");
        List<String> groupList = (List<String>) param.get("groupList");


        EntityTable entity = EntityUtil.getEntityTable(m);

        SQL sql = new SQL()
                .SELECT(entity.getSqlColumns())
                .FROM(entity.getTableName())
                .WHERE("1 = 1");


        getWhere(m, sql, entity);


        getGroupSql(groupList, sql);


        getOrderSql(sortMap, sql, entity);

        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }


}
